
<?php
include 'config.php';
function GetLevelByName($name, $conn) {
    $sql = "SELECT * FROM userdata where bind_user = '$name'";
    $result = $conn->query($sql);
    $count = $result->fetch_assoc()['level'];
    $sun = str_repeat("☀️", floor($count / 27));
    $moon = str_repeat("🌙", floor($count / 9) % 3);
    $star = str_repeat("⭐️", floor($count / 3) % 3);
    return $sun . $moon . $star;
}

$sql = "SELECT nickname, message, created_at FROM messages ORDER BY created_at ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<div class='message'>";
        echo GetLevelByName($row['nickname'], $conn);
        echo '<a href="user/user_centre.php?nickname=' . htmlspecialchars($row["nickname"]) . '">' . $row["nickname"] . "</a>:";
        echo "<span class='text'>" . $row["message"] . "</span>";
        echo "<span class='timestamp'>" . $row["created_at"] . "</span>";
        echo "</div>";
    }
} else {
    echo "<p>暂无消息</p>";
}

$conn->close();
?>

